package com.example.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.myblog.entity.Argue;
import com.example.myblog.entity.ArgueReply;
import com.example.myblog.mapper.ArgueMapper;
import com.example.myblog.mapper.ArgueReplyMapper;
import com.example.myblog.service.IArgueReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.common.biz.BizException;
import com.yc.common.util.Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 廖老师
 * @since 2023-06-30
 */
@Service
public class ArgueReplyServiceImpl extends ServiceImpl<ArgueReplyMapper, ArgueReply> implements IArgueReplyService {

    @Resource
    ArgueMapper argueMapper;

    @Transactional
    @Override
    public void submit(ArgueReply reply) throws BizException {
        // 只能评论一次
        LambdaQueryWrapper<ArgueReply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ArgueReply::getAid, reply.getAid());
        lqw.eq(ArgueReply::getUid, reply.getUid());
        final long count = this.count(lqw);
        Utils.check(count>0,"您已经发布了评论!");

        // 更新正反方数量
        final Argue argue = argueMapper.selectById(reply.getAid());
        if(reply.getApprove().equals("positive")){
            argue.setpNums(argue.getpNums()+1);
        } else {
            argue.setnNums(argue.getnNums()+1);
        }
        argueMapper.updateById(argue);

        // 保存记录
        this.save(reply);
    }
}
